#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int ACMPerk = 0;

char OriginalCity[20] = {'\0'};
char DistanceCity[20] = {'\0'};
int  ActualMiles = 0;
char ClassCode = ' ';

void process()
{
    //printf("%s %s %d %c\n", OriginalCity, DistanceCity, ActualMiles, ClassCode);

    switch(ClassCode)
    {
        case 'F':
        ACMPerk += ActualMiles * 2;
        break;
        case 'B':
        ACMPerk += (ActualMiles) * 1.5 + 0.5;
        break;
        case 'Y':
        if(ActualMiles <= 500)
        {
            ACMPerk += 500;
        }
        else
        {
            ACMPerk += ActualMiles;
        }
        break;
        default:
        break;
    }
}


int main()
{
    //freopen("bin\\debug\\in.txt", "r", stdin);

    while(1)
    {
        scanf("%s", OriginalCity);
        if(strcmp(OriginalCity, "#") == 0)
        {
            break;
        }
        if(strcmp(OriginalCity, "0") == 0)
        {
            printf("%d\n", ACMPerk);
            ACMPerk = 0;
            continue;
        }
        scanf("%s", DistanceCity);
        scanf("%d", &ActualMiles);
        getchar();
        scanf("%c", &ClassCode);
        process();

    }


    return 0;
}
